package models

import (
	"gitee.com/laofa1/play/k_conf_mng/kconf_backend/pkg/util"
	"log"
	"strconv"
)

type ConfGetToken struct {
	ID uint64 `json:"id"`
	Name string `json:"name"`
	Content string `json:"value"`
	UserID uint64
}

func GetConfToken(userID uint64) []ConfGetToken {
	var confGetToken []ConfGetToken
	if result := db.Where(ConfGetToken{UserID: userID}).Find(&confGetToken); result.Error != nil {
		log.Println(result.Error)
		return make([]ConfGetToken, 0)
	}
	return confGetToken
}


func GenerateConfToken(userID uint64, name string) error {
	token, err := util.GenerateToken(strconv.Itoa(int(userID)))
	if err != nil {
		log.Println(err)
		return err
	}

	confToken := ConfGetToken{Name: name, Content: token, UserID: userID}

	if result := db.Create(&confToken); result.Error != nil {
		return result.Error
	} else {
		return nil
	}
}

func DeleteConfToken(userID, id uint64) error {
	token := ConfGetToken{ID: id, UserID: userID}
	if result := db.Delete(token); result.Error != nil {
		return result.Error
	}
	return nil
}